package com.example.backendmanage.controller;

import com.example.backendmanage.common.AjaxResult;
import com.example.backendmanage.info.Menu;
import com.example.backendmanage.info.MenuRole;
import com.example.backendmanage.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    MenuMapper menuMapper;

    @RequestMapping("/findAll")
    public AjaxResult findAll(){
        List<Menu> list=menuMapper.findAll();
        //找到list中pid为null的
        List<Menu> plist=list.stream().filter(menu -> menu.getPid()==null).collect(Collectors.toList());
        //找到plist里面所有的子节点
        for (Menu m :
                plist) {
            //根据父ID找到子ID
            m.setChildren(list.stream().filter(mm -> m.getId().equals(mm.getPid())).collect(Collectors.toList()));
        }
        HashMap<String,Object> hashMap=new HashMap<>();
        hashMap.put("result",plist);
        return AjaxResult.success(hashMap);
    }

    @RequestMapping("/addMenu")
    public AjaxResult addMenu(@RequestBody Menu menu){
        return AjaxResult.success(menuMapper.addMenu(menu));
    }

    @RequestMapping("/addMenuRole/{roleId}")
    public AjaxResult addMenuRole(@PathVariable Integer roleId,@RequestBody Map<String, Object> requestData){
        List<Integer> menuIds = (List<Integer>) requestData.get("menuIds");
        menuMapper.deleteMR(roleId);
        for (Integer MenuId :
                menuIds) {
            MenuRole m = new MenuRole();
            m.setMenuId(MenuId);
            m.setRoleId(roleId);
            menuMapper.addMR(m);
        }
        return AjaxResult.success(true);
    }

    @GetMapping("/showMenuRole/{roleId}")
    public AjaxResult showMenuRole(@PathVariable Integer roleId){
        List<Integer> selectMenuByRoleIds=menuMapper.selectMenuByRoleId(roleId);
        HashMap<String,Object> hashMap=new HashMap<>();
        hashMap.put("result",selectMenuByRoleIds);
        return AjaxResult.success(hashMap);
    }

    @GetMapping("/ShowRoleMenu")
    public AjaxResult ShowRoleMenu(@RequestParam String role){
        Integer roleId=menuMapper.selectRoleIdByRole(role);
        //找到roleId所对应的menuId
        List<Integer> menuIds=menuMapper.selectMenuByRoleId(roleId);

        for (Integer menuId :
                menuIds) {
            //查到pid是否非空
            Integer pid=menuMapper.getPid(menuId);
            System.out.println("pid->"+pid);
            if (pid!=null){
                //若不为空则查看menu role关系表里面是否插入数据
                Integer count=menuMapper.checkPidExist(roleId,pid);
                if (count<=0){
                    menuMapper.addLostMenuRole(roleId,pid);
                }
            }
        }

        List<Menu> FindMenus=new ArrayList<>();
        for (Integer menuId :
                menuIds) {
            Menu m= menuMapper.selectMenuByMenuId(menuId);
            FindMenus.add(m);
        }
        List<Menu> plist=FindMenus.stream().filter(menu -> menu.getPid()==null).collect(Collectors.toList());
        for (Menu tempMenu :
                plist) {
            tempMenu.setChildren(FindMenus.stream().filter(mm -> tempMenu.getId().equals(mm.getPid())).collect(Collectors.toList()));
        }
        HashMap<String,Object> hashMap=new HashMap<>();
        hashMap.put("result",plist);
        return AjaxResult.success(hashMap);
    }
}
